<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  $languages = tep_get_languages();

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  $reseller_page = (isset($HTTP_GET_VARS['reseller_page']) && is_numeric($HTTP_GET_VARS['reseller_page'])) ? $HTTP_GET_VARS['reseller_page'] : 1;
  $city_page = (isset($HTTP_GET_VARS['city_page']) && is_numeric($HTTP_GET_VARS['city_page'])) ? $HTTP_GET_VARS['city_page'] : 1;
  $place_page = (isset($HTTP_GET_VARS['place_page']) && is_numeric($HTTP_GET_VARS['place_page'])) ? $HTTP_GET_VARS['place_page'] : 1;

  $page_info = 'reseller_page=' . $reseller_page . '&city_page=' . $city_page . '&place_page=' . $place_page;

  if (tep_not_null($action)) {
    switch ($action) {
      case 'add_reseller_places':
        $reseller_id = tep_db_prepare_input($HTTP_POST_VARS['reseller_id']);
        $city_id     = tep_db_prepare_input($HTTP_POST_VARS['city_id']);
        $products_id = tep_db_prepare_input($HTTP_POST_VARS['products_id']);
        $custom_rate = tep_db_prepare_input($HTTP_POST_VARS['custom_rate']);
        $rate_prefix = tep_db_prepare_input($HTTP_POST_VARS['rate_prefix']);

        tep_db_query("insert into " . TABLE_RESELLERS_PLACES . " (reseller_place_id, reseller_id, city_id, products_id, custom_rate, rate_prefix ) values (null, '" . (int)$reseller_id . "', '" . (int)$city_id . "', '" . (int)$products_id . "', '" . (float)tep_db_input($custom_rate) . "', '" . tep_db_input($rate_prefix) . "')");

        tep_redirect(tep_href_link(FILENAME_RESELLER_PLACES, $page_info));
        break;
      case 'update_reseller_places':
        $reseller_id = tep_db_prepare_input($HTTP_POST_VARS['reseller_id']);
        $city_id     = tep_db_prepare_input($HTTP_POST_VARS['city_id']);
        $products_id = tep_db_prepare_input($HTTP_POST_VARS['products_id']);
        $custom_rate = tep_db_prepare_input($HTTP_POST_VARS['custom_rate']);
        $rate_prefix = tep_db_prepare_input($HTTP_POST_VARS['rate_prefix']);
        $place_id    = tep_db_prepare_input($HTTP_POST_VARS['place_id']);

        tep_db_query("update " . TABLE_RESELLERS_PLACES . " set products_id = '" . (int)$products_id . "', reseller_id = '" . (int)$reseller_id . "', city_id = '" . (int)$city_id . "', custom_rate = '" . (float)tep_db_input($custom_rate) . "', rate_prefix = '" . tep_db_input($rate_prefix) . "' where reseller_place_id = '" . (int)$place_id . "'");

        tep_redirect(tep_href_link(FILENAME_RESELLER_PLACES, $page_info));
        break;
      case 'delete_reseller_place':
        $place_id = tep_db_prepare_input($HTTP_GET_VARS['place_id']);

        tep_db_query("delete from " . TABLE_RESELLERS_PLACES . " where reseller_place_id = '" . (int)$place_id . "'");

        tep_redirect(tep_href_link(FILENAME_RESELLER_PLACES, $page_info));
        break;
    }
  }

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td class="smallText">&nbsp;</td>
      </tr>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">&nbsp;<?php echo HEADING_TITLE_PLACES; ?>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
      <tr>
<?php
  if ($action == 'update_place') {
    $form_action = 'update_reseller_places';
  } else {
    $form_action = 'add_reseller_places';
  }
?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td class="smallText" align="right">
<?php

  $places = "select pla.* from " . TABLE_RESELLERS_PLACES . " pla left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on pla.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by pd.products_name, pla.reseller_id, pla.products_id";
  $places_split = new splitPageResults($place_page, MAX_ROW_LISTS_OPTIONS, $places, $places_query_numrows);

  echo $places_split->display_links($places_query_numrows, MAX_ROW_LISTS_OPTIONS, MAX_DISPLAY_PAGE_LINKS, $place_page, 'reseller_page=' . $reseller_page . '&city_page=' . $city_page, 'place_page');
?>
            </td>
          </tr>
        </table>
        <form name="places" action="<?php echo tep_href_link(FILENAME_RESELLER_PLACES, 'action=' . $form_action . '&' . $page_info); ?>" method="post">
        	<table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="7"><?php echo tep_black_line(); ?></td>
          </tr>
          <tr class="dataTableHeadingRow">
            <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_ID; ?>&nbsp;</td>
            <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_RESELLER; ?>&nbsp;</td>
            <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_PRODUCT; ?>&nbsp;</td>
            <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_CITY; ?>&nbsp;</td>
            <td class="dataTableHeadingContent" align="right">&nbsp;<?php echo TABLE_HEADING_CUSTOM_RATE; ?>&nbsp;</td>
            <td class="dataTableHeadingContent" align="center">&nbsp;<?php echo TABLE_HEADING_PREFIX; ?>&nbsp;</td>
            <td class="dataTableHeadingContent" align="center">&nbsp;<?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
          </tr>
          <tr>
            <td colspan="7"><?php echo tep_black_line(); ?></td>
          </tr>
<?php
  $next_id = 1;
  $places = tep_db_query($places);
  while ($places_values = tep_db_fetch_array($places)) {
    $products_name_only = tep_get_products_name($places_values['products_id']);
    $reseller_name = tep_reseller_name($places_values['reseller_id']);
    $city_name = tep_city_name($places_values['city_id']);
    $rows++;
?>
          <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
<?php
    if (($action == 'update_place') && ($HTTP_GET_VARS['place_id'] == $places_values['reseller_place_id'])) {
?>
            <td class="smallText">&nbsp;<?php echo $places_values['reseller_place_id']; ?><input type="hidden" name="place_id" value="<?php echo $places_values['reseller_place_id']; ?>">&nbsp;</td>
            <td class="smallText">&nbsp;<select name="reseller_id">
<?php
      $reseller = tep_db_query("select reseller_id, reseller_name from " . TABLE_RESELLERS . " order by reseller_name");
      while($reseller_values = tep_db_fetch_array($reseller)) {
        if ($places_values['reseller_id'] == $reseller_values['reseller_id']) {
          echo "\n" . '<option name="' . $reseller_values['reseller_name'] . '" value="' . $reseller_values['reseller_id'] . '" SELECTED>' . $reseller_values['reseller_name'] . '</option>';
        } else {
          echo "\n" . '<option name="' . $reseller_values['reseller_name'] . '" value="' . $reseller_values['reseller_id'] . '">' . $reseller_values['reseller_name'] . '</option>';
        }
      } 
?>
            </select>&nbsp;</td>
            <td class="smallText">&nbsp;<select name="products_id">
<?php
      $products = tep_db_query("select p.products_id, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.products_id = p.products_id and pd.language_id = '" . $languages_id . "' order by pd.products_name");
      while($products_values = tep_db_fetch_array($products)) {
        if ($places_values['products_id'] == $products_values['products_id']) {
          echo "\n" . '<option name="' . $products_values['products_name'] . '" value="' . $products_values['products_id'] . '" SELECTED>' . $products_values['products_name'] . '</option>';
        } else {
          echo "\n" . '<option name="' . $products_values['products_name'] . '" value="' . $products_values['products_id'] . '">' . $products_values['products_name'] . '</option>';
        }
      } 
?>
            </select>&nbsp;</td>
            <td class="smallText">&nbsp;<select name="city_id">
<?php
      $city = tep_db_query("select CONCAT( b.zone_code,  "."' - '".", a.city_name ) as city_name, a.city_id from " . TABLE_ZONES . " b, " . TABLE_CITIES . " a where a.city_zone_id = b.zone_id order by 1");
      while($city_values = tep_db_fetch_array($city)) {
        if ($places_values['city_id'] == $city_values['city_id']) {
          echo "\n" . '<option name="' . $city_values['city_name'] . '" value="' . $city_values['city_id'] . '" SELECTED>' . $city_values['city_name'] . '</option>';
        } else {
          echo "\n" . '<option name="' . $city_values['city_name'] . '" value="' . $city_values['city_id'] . '">' . $city_values['city_name'] . '</option>';
        }
      } 
?>        
            </select>&nbsp;</td>
            <td align="right" class="smallText">&nbsp;<input type="text" name="custom_rate" value="<?php echo $places_values['custom_rate']; ?>" size="6">&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<input type="text" name="rate_prefix" value="<?php echo $places_values['rate_prefix']; ?>" size="2">&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<?php echo tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_RESELLER_PLACES, $page_info, 'NONSSL')); ?>&nbsp;</td>
<?php
    } elseif (($action == 'delete_place') && ($HTTP_GET_VARS['place_id'] == $places_values['reseller_place_id'])) {
?>
            <td class="smallText">&nbsp;<strong><?php echo $places_values["reseller_place_id"]; ?></strong>&nbsp;</td>
            <td class="smallText">&nbsp;<strong><?php echo $reseller_name; ?></strong>&nbsp;</td>
            <td class="smallText">&nbsp;<strong><?php echo $products_name_only; ?></strong>&nbsp;</td>
            <td class="smallText">&nbsp;<strong><?php echo $city_name; ?></strong>&nbsp;</td>
            <td align="right" class="smallText">&nbsp;<strong><?php echo $places_values["cutom_rate"]; ?></strong>&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<strong><?php echo $places_values["rate_prefix"]; ?></strong>&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<?php echo tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_RESELLER_PLACES, 'action=delete_reseller_place&place_id=' . $HTTP_GET_VARS['place_id'] . '&' . $page_info), 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_RESELLER_PLACES, $page_info, 'NONSSL')); ?>&nbsp;</td>
<?php
    } else {
?>
            <td class="smallText">&nbsp;<?php echo $places_values["reseller_place_id"]; ?>&nbsp;</td>
            <td class="smallText">&nbsp;<?php echo $reseller_name; ?>&nbsp;</td>
            <td class="smallText">&nbsp;<?php echo $products_name_only; ?>&nbsp;</td>
            <td class="smallText">&nbsp;<?php echo $city_name; ?>&nbsp;</td>
            <td align="right" class="smallText">&nbsp;<?php echo $places_values["custom_rate"]; ?>&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<?php echo $places_values["rate_prefix"]; ?>&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<?php echo tep_draw_button(IMAGE_EDIT, 'document', tep_href_link(FILENAME_RESELLER_PLACES, 'action=update_place&place_id=' . $places_values['reseller_place_id'] . '&' . $page_info, 'NONSSL')) . tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_RESELLER_PLACES, 'action=delete_place&place_id=' . $places_values['reseller_place_id'] . '&' . $page_info, 'NONSSL')); ?>&nbsp;</td>
<?php
    }
    $max_place_id_query = tep_db_query("select max(reseller_place_id) + 1 as next_id from " . TABLE_RESELLERS_PLACES);
    $max_place_id_values = tep_db_fetch_array($max_place_id_query);
    $next_id = $max_place_id_values['next_id'];
?>
          </tr>
<?php
  }
  if ($action != 'update_place') {
?>
          <tr>
            <td colspan="7"><?php echo tep_black_line(); ?></td>
          </tr>
          <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
            <td class="smallText">&nbsp;<?php echo $next_id; ?>&nbsp;</td>
      	    <td class="smallText">&nbsp;<select name="reseller_id">
<?php
    $reseller = tep_db_query("select reseller_id, reseller_name from " . TABLE_RESELLERS . " order by reseller_name");
    while ($reseller_values = tep_db_fetch_array($reseller)) {
      echo '<option name="' . $reseller_values['reseller_name'] . '" value="' . $reseller_values['reseller_id'] . '">' . $reseller_values['reseller_name'] . '</option>';
    } 
?>
            </select>&nbsp;</td>
            <td class="smallText">&nbsp;<select name="products_id">
<?php
    $products = tep_db_query("select p.products_id, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.products_id = p.products_id and pd.language_id = '" . $languages_id . "' order by pd.products_name");
    while ($products_values = tep_db_fetch_array($products)) {
      echo '<option name="' . $products_values['products_name'] . '" value="' . $products_values['products_id'] . '">' . $products_values['products_name'] . '</option>';
    } 
?>
            </select>&nbsp;</td>
            <td class="smallText">&nbsp;<select name="city_id">
<?php
    $city = tep_db_query("select CONCAT( b.zone_code,  "."' - '".", a.city_name ) as city_name, a.city_id from " . TABLE_ZONES . " b, " . TABLE_CITIES . " a where a.city_zone_id = b.zone_id order by 1");
    while ($city_values = tep_db_fetch_array($city)) {
      echo '<option name="' . $city_values['city_name'] . '" value="' . $city_values['city_id'] . '">' . $city_values['city_name'] . '</option>';
    } 
?>
            </select>&nbsp;</td>
            <td align="right" class="smallText">&nbsp;<input type="text" name="custom_rate" size="6">&nbsp;</td>
            <td align="right" class="smallText">&nbsp;<input type="text" name="rate_prefix" size="2" value="+">&nbsp;</td>
            <td align="center" class="smallText">&nbsp;<?php echo tep_draw_button(IMAGE_INSERT, 'plus'); ?>&nbsp;</td>
          </tr>
<?php
  }
?>
          <tr>
            <td colspan="7"><?php echo tep_black_line(); ?></td>
          </tr>
        </table>
        </form>
        </td>
      </tr>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
